namespace OpenComposite.EII.Repository
{
	using System;
	using System.CodeDom.Compiler;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Diagnostics;
	using System.Drawing;
	using System.IO;
	using System.Text;
	using System.Windows.Forms;
	using System.Xml;
	using System.Xml.Schema;
	using System.Xml.Serialization;

	using OpenComposite.Base;
	using OpenComposite.Repository.Properties;

	[RepositoryItem(ItemType.BPolicy, "business_policies", "bpolicy_id")]
	public class BusinessPolicy : RepositoryItemBaseEx
	{
		#region Initialization

		/// <summary>
		/// Initializes a new instance of the <see cref="BusinessPolicy"/> class.
		/// </summary>
		public BusinessPolicy()
			: base("bpolicy_id", "bpolicy_nm", "bpolicy_desc", "bpolicy_ntext", Resources.sNewBPolicy)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="BusinessPolicy"/> class.
		/// </summary>
		/// <param name="bCreateNew">if set to <c>true</c> [b create new].</param>
		public BusinessPolicy(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="BusinessPolicy"/> class.
		/// </summary>
		/// <param name="id">The ID.</param>
		public BusinessPolicy(int id)
			: this()
		{
			this.Load(id);
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		/// <summary>
		/// Gets or sets the action description.
		/// </summary>
		/// <value>The action description.</value>
		public string ActionDescription
		{
			get { return getString("bpolicy_actiondesc", ""); }
			set { setValue("ActionDescription", "bpolicy_actiondesc", value); }
		}

		/// <summary>
		/// Gets or sets the actions.
		/// </summary>
		/// <value>The actions.</value>
		public ActionPolicy Actions
		{
			get
			{
				if ( _actions == null )
					_actions = getXmlSerializedObject<ActionPolicy>("bpolicy_action");
				return _actions;
			}
			set
			{
				_actions = value;
				setXmlSerializedObject("Actions", "bpolicy_action", value);
			}
		}

		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return _LISTALLOWED; }
		}

		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override Image Image
		{
			get
			{
				Bitmap bmp = Resources.policy;
				bmp.MakeTransparent(Color.Magenta);
				return bmp;
			}
		}

		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get { return "policy"; }
		}

		/// <summary>
		/// Gets or sets the policy code.
		/// </summary>
		/// <value>The policy code.</value>
		public string PolicyCode
		{
			get { return getString("bpolicy_code", ""); }
			set { setValue("PolicyCode", "bpolicy_code", value); }
		}

		/// <summary>
		/// Gets or sets the policy manager.
		/// </summary>
		/// <value>The policy manager.</value>
		public string PolicyManager
		{
			get { return getString("bpolicy_manager", ""); }
			set { setValue("PolicyManager", "bpolicy_manager", value); }
		}

		/// <summary>
		/// Gets or sets the policy result strings.
		/// </summary>
		/// <value>The policy result strings.</value>
		public List<string> PolicyResultStrings
		{
			get { return getXmlSerializedObject<List<string>>("bpolicy_resultstrings"); }
			set { setXmlSerializedObject("PolicyResultStrings", "bpolicy_resultstrings", value); }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.BPolicy; }
		}

		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return Resources.sBPolicy; }
		}

		/// <summary>
		/// Gets or sets the rule description.
		/// </summary>
		/// <value>The rule description.</value>
		public string RuleDescription
		{
			get { return getString("bpolicy_ruledesc", ""); }
			set { setValue("RuleDescription", "bpolicy_ruledesc", value); }
		}

		/// <summary>
		/// Gets or sets the rules.
		/// </summary>
		/// <value>The rules.</value>
		public RulePolicy Rules
		{
			get
			{
				if ( _rules == null )
					_rules = getXmlSerializedObject<RulePolicy>("bpolicy_rule");
				return _rules;
			}
			set
			{
				_rules = value;
				setXmlSerializedObject("Rules", "bpolicy_rule", value);
			}
		}

		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override Image SelectedImage
		{
			get { return this.Image; }
		}

		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return "policy"; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "business_policies"; }
		}
		#endregion Properties

		#region Methods

		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override Icon GetIcon()
		{
			Bitmap bmp = new Bitmap(this.Image);
			bmp.MakeTransparent(Color.Magenta);
			return Icon.FromHandle(bmp.GetHicon());
		}

		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			BusinessPolicy copy = new BusinessPolicy(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			XmlDocument xdDiagram = this.Diagram;
			foreach ( var rule in Global.Data.GetRepositoryHierarchyChildRepositoryList<RepositoryBusinessRule>(this) ) {
				RepositoryBusinessRule copyRule = rule.Copy(deep) as RepositoryBusinessRule;
				copyRule.BeginEdit();
				copyRule.Name = rule.Name;
				copyRule.EndEdit(true);
				XmlNode xnRule = xdDiagram.SelectSingleNode("//DesignerControl/RepositoryBusinessRule[@id=" + rule.ID + "]");
				xnRule.Attributes["id"].Value = copyRule.ID.ToString();
				xnRule.Attributes["name"].Value = copyRule.Name;
				xnRule.ParentNode.SelectSingleNode("./Name").InnerText = copyRule.Name;
				xnRule.ParentNode.SelectSingleNode("./Text").InnerText = copyRule.Name;
				Global.Data.AddRepositoryHierarchy(copy, copyRule, Global.Status.CurrentRepositoryId);
			}
			copy.Diagram = xdDiagram;
			copy.Save();

			return copy;
		}

		#endregion Methods

		#endregion Public Members

		#region Internal Members

		#region Constant/Read-Only Fields

		internal const string ColumnDescription = "bpolicy_desc";
		internal const string ColumnID = "bpolicy_id";
		internal const string ColumnNText = "bpolicy_ntext";
		internal const string ColumnName = "bpolicy_nm";
		internal const string cParticipantPolicy = "ParticipantPolicy";

		#endregion Constant/Read-Only Fields

		#endregion Internal Members

		#region Private Members

		#region Static Fields

		private static List<ItemType> _LISTALLOWED = new List<ItemType>(new ItemType[] { ItemType.BRule });

		#endregion Static Fields

		#region Fields

		private ActionPolicy _actions = null;
		private RulePolicy _rules = null;

		#endregion Fields

		#endregion Private Members

		#region Nested Types

		/// <remarks/>
		[GeneratedCodeAttribute("xsd", "2.0.50727.42")]
		[SerializableAttribute]
		[DebuggerStepThroughAttribute]
		[DesignerCategoryAttribute("code")]
		[XmlTypeAttribute(Namespace = "http://www.theopencomposite.com/Schemas/ActionPolicy")]
		public partial class Action
		{
			#region Public Members

			#region Properties

			/// <remarks/>
			public string ElseAction
			{
				get
				{
					return this.elseActionField;
				}
				set
				{
					this.elseActionField = value;
				}
			}

			/// <remarks/>
			public string IfOperator
			{
				get
				{
					return this.ifOperatorField;
				}
				set
				{
					this.ifOperatorField = value;
				}
			}

			/// <remarks/>
			public string IfVar
			{
				get
				{
					return this.ifVarField;
				}
				set
				{
					this.ifVarField = value;
				}
			}

			/// <remarks/>
			public string IfVar1
			{
				get
				{
					return this.ifVar1Field;
				}
				set
				{
					this.ifVar1Field = value;
				}
			}

			/// <remarks/>
			public string Name
			{
				get
				{
					return this.nameField;
				}
				set
				{
					this.nameField = value;
				}
			}

			/// <remarks/>
			public string ThenAction
			{
				get
				{
					return this.thenActionField;
				}
				set
				{
					this.thenActionField = value;
				}
			}

			#endregion Properties

			#endregion Public Members

			#region Private Members

			#region Fields

			private string elseActionField;
			private string ifOperatorField;
			private string ifVar1Field;
			private string ifVarField;
			private string nameField;
			private string thenActionField;

			#endregion Fields

			#endregion Private Members
		}

		//
		// This source code was auto-generated by xsd, Version=2.0.50727.42.
		//
		/// <remarks/>
		[GeneratedCodeAttribute("xsd", "2.0.50727.42")]
		[SerializableAttribute]
		[DebuggerStepThroughAttribute]
		[DesignerCategoryAttribute("code")]
		[XmlTypeAttribute(AnonymousType = true)]
		[XmlRootAttribute(Namespace = "http://www.theopencomposite.com/Schemas/ActionPolicy", IsNullable = false)]
		public partial class ActionPolicy
		{
			#region Initialization

			public ActionPolicy()
			{
				this.Actions = new List<Action>();
			}

			#endregion Initialization

			#region Public Members

			#region Properties

			/// <remarks/>
			[XmlElementAttribute("Actions")]
			public List<Action> Actions
			{
				get
				{
					return this.actionsField;
				}
				set
				{
					this.actionsField = value;
				}
			}

			#endregion Properties

			#endregion Public Members

			#region Private Members

			#region Fields

			private List<Action> actionsField;

			#endregion Fields

			#endregion Private Members
		}

		/// <remarks/>
		[GeneratedCodeAttribute("xsd", "2.0.50727.42")]
		[SerializableAttribute]
		[DebuggerStepThroughAttribute]
		[DesignerCategoryAttribute("code")]
		[XmlTypeAttribute(Namespace = "http://www.theopencomposite.com/Schemas/RulePolicy")]
		public partial class Rule
		{
			#region Public Members

			#region Properties

			/// <remarks/>
			public string ElseFact
			{
				get
				{
					return this.elseFactField;
				}
				set
				{
					this.elseFactField = value;
				}
			}

			/// <remarks/>
			public string IfOperator
			{
				get
				{
					return this.ifOperatorField;
				}
				set
				{
					this.ifOperatorField = value;
				}
			}

			/// <remarks/>
			public string IfVar1
			{
				get
				{
					return this.ifVar1Field;
				}
				set
				{
					this.ifVar1Field = value;
				}
			}

			/// <remarks/>
			public string IfVar2
			{
				get
				{
					return this.ifVar2Field;
				}
				set
				{
					this.ifVar2Field = value;
				}
			}

			/// <remarks/>
			public string Name
			{
				get
				{
					return this.nameField;
				}
				set
				{
					this.nameField = value;
				}
			}

			/// <remarks/>
			public string ThenFact
			{
				get
				{
					return this.thenFactField;
				}
				set
				{
					this.thenFactField = value;
				}
			}

			#endregion Properties

			#endregion Public Members

			#region Private Members

			#region Fields

			private string elseFactField;
			private string ifOperatorField;
			private string ifVar1Field;
			private string ifVar2Field;
			private string nameField;
			private string thenFactField;

			#endregion Fields

			#endregion Private Members
		}

		//
		// This source code was auto-generated by xsd, Version=2.0.50727.42.
		//
		/// <remarks/>
		[GeneratedCodeAttribute("xsd", "2.0.50727.42")]
		[SerializableAttribute]
		[DebuggerStepThroughAttribute]
		[DesignerCategoryAttribute("code")]
		[XmlTypeAttribute(AnonymousType = true)]
		[XmlRootAttribute(Namespace = "http://www.theopencomposite.com/Schemas/RulePolicy", IsNullable = false)]
		public partial class RulePolicy
		{
			#region Initialization

			public RulePolicy()
			{
				this.Rules = new List<Rule>();
			}

			#endregion Initialization

			#region Public Members

			#region Properties

			/// <remarks/>
			[global::System.Xml.Serialization.XmlElementAttribute("Rules")]
			public List<Rule> Rules
			{
				get
				{
					return this.rulesField;
				}
				set
				{
					this.rulesField = value;
				}
			}

			#endregion Properties

			#endregion Public Members

			#region Private Members

			#region Fields

			private List<Rule> rulesField;

			#endregion Fields

			#endregion Private Members
		}

		#endregion Nested Types
	}
}